package problemloesen.projekt01;

import java.util.Random;




public class Pointperl extends Point{
	
	public static int number;
	public static double weight;
	public static double length;
	public static double ebeiwert;
	public static double distance;
	
	private double [] dlengthValues;
	
	private Random rnd;
	private final double GAUSS_MULT = 1.0;
		
	public Pointperl(){
		
	}
	
	public Pointperl (double[] values)
	{
		this.values = values;
	}
	
	public double calcSpringConstant(){
		return ebeiwert/length;
	}
	
	public double calcdlengthright(double[] values,int counter){
		double temp;
		temp = calclengthright(values,counter)-length;
		if(temp <= 0){
			temp = 0;
		}
		return temp;
	}
	
	public double calclengthright(double[] values, int counter){
			double lengthleft = Math.sqrt(Math.pow(values[counter]-values[counter+2],2)+Math.pow(values[counter+1]-values[counter+3],2));
		return lengthleft;
	}
	
	public double eval() {	
		
		double max = 0;
		double temp;
		temp = Math.sqrt(Math.pow(this.values[0]-0,2)+Math.pow(this.values[1]-0,2))-length;
		if (temp <= 0){
			temp = 0;
		}
			max = 0.5*calcSpringConstant()*Math.pow(temp,2); 
			for(int i = 0 ; i < number*2-2 ; i = i+2 ){
				max = max+0.5*calcSpringConstant()*Math.pow(calcdlengthright(this.values,i),2)+weight*9.81*(this.values[i+1]);
			}
		temp = Math.sqrt(Math.pow(this.values[number*2-2]-distance,2)+Math.pow(this.values[number*2-1]-0,2))-length;
		if (temp <= 0){
			temp = 0;
		}
			max = max+0.5*calcSpringConstant()*Math.pow(temp,2)+weight*9.81*(this.values[number*2-1]);
		return max;
	}
	
	public double[] calcdlengthleftforallpoints(double[] values){
		double dlength[] = new double[(values.length/2)+2] ;
		
		int ix = 1;
			dlength[0] = Math.sqrt(Math.pow(values[0]-0,2)+Math.pow(values[1]-0,2))-length;
		for(int i =0;i<number*2-2;i=i+2){
			dlength[ix++] = calclengthright(values,i)-length;
		}
			dlength[values.length/2] = Math.sqrt(Math.pow(values[number*2-2]-distance,2)+Math.pow(values[number*2-1]-0,2))-length;
		return dlength;
	}

	public double[] getDlengthValues() {
		return dlengthValues;
	}

	public void setDlengthValues(double[] dlengthValues) {
		this.dlengthValues = dlengthValues;
	}

	public Point clone()
	{
		return new Pointperl(this.values.clone());
	}
	
	public Point gaussianClone()
	{
		double []newValues = new double[this.values.length];
		for (int i = 0; i<newValues.length;i++)
		{
			if (rnd == null) rnd = new Random(System.currentTimeMillis());
			newValues[i] += (rnd.nextGaussian() * GAUSS_MULT);
		}
		return new Pointperl(newValues);
	}

	
	
	
}
